/*
 * OpenParts
 * A dynamic-loading components framework for GWT
 * Copyright (C) 2011 Christophe Bouthier  [chris{AT}binary-gastronome{DOT}fr]
 *
 * This work is partially based on work I have done at INRIA (http://www.inria.fr) 
 * in the context of the Qualipso European Project (http://qualipso.org/),
 * The work done at INRIA is Copyright (C) 2006-2011 INRIA
 *
 * This work is distributed under the LGPL version 3
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation. See the GNU
 * Lesser General Public License in LGPL.txt for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package fr.openparts.opshowcase.messageembedded.client.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

import fr.openparts.OpenParts.client.OPCommandCallback;
import fr.openparts.OpenParts.client.OPCommandFailureReason;
import fr.openparts.OpenParts.client.OPMessageFromContainerSlotHandler;
import fr.openparts.OpenParts.client.OPParams;
import fr.openparts.OpenParts.client.OPSlotProxy;
import fr.openparts.opshowcase.messageembedded.client.presenter.MessageEmbeddedPresenter;
import fr.openparts.opshowcase.messageembedded.client.resources.MessageEmbeddedResources;

/**
 * View of a Embedded view for Messages Dispatching for OpenParts Showcase.
 * 
 * @author Christophe Bouthier [chris{AT}binary-gastronome{DOT}fr]
 * @creation.date 26 January 2011
 */
public class MessageEmbeddedView extends Composite {

    interface MessageEmbeddedViewUiBinder extends UiBinder<Widget, MessageEmbeddedView> {}
    private static MessageEmbeddedViewUiBinder uiBinder = GWT.create(MessageEmbeddedViewUiBinder.class);

    protected MessageEmbeddedPresenter presenter;

    @UiField
    protected MessageEmbeddedResources resources;
    
    @UiField
    protected Label title;
    @UiField
    protected Label message;
    @UiField
    protected Button sendMessage;
    @UiField
    protected Label callback;
    
    protected String id;
    protected OPSlotProxy proxy;
    
    /**
     * Constructor.
     */
    public MessageEmbeddedView(final MessageEmbeddedPresenter presenter, final String id) {
        initWidget(uiBinder.createAndBindUi(this));
        resources.style().ensureInjected();
        this.presenter = presenter;
        this.id = id;
        title.setText(title.getText() + " " + id);
        sendMessage.setEnabled(false);
        message.setText("no message from container " + id + " yet");
    }
    
    /**
     * Send a message to the container
     */
    @UiHandler("sendMessage")
    public void sendMessage(final ClickEvent event) {
        proxy.dispatchMessageToContainerSlot("message from view " + id, new OPParams("id", id), new OPCommandCallback() {
            @Override
            public void onSuccess() {
                callback.setText("view dispatch success " + id);
            }
            @Override
            public void onFail(OPCommandFailureReason reason) {
                callback.setText("view dispatch failure " + id + " : " + reason.getType());
            }
        });
    }
    
    /**
     * Set the slot proxy for this embedded view.
     */
    public void setProxy(final OPSlotProxy proxy) {
        this.proxy = proxy;
        sendMessage.setEnabled(true);
        proxy.addMessageHandler(new OPMessageFromContainerSlotHandler() {
            @Override
            public void handleMessageFromContainerSlot(String msg, OPParams params) {
                if ((params != null) && params.getParamValue("id") != null) {
                    message.setText(msg + " params = " + params.getParamValue("id"));
                } else {
                    message.setText(msg);
                }
            }
            @Override
            public void onEmbeddedViewAttach() {
            }
        });
    }

}
